package com.example.xungeng2.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.xungeng2.mapper.TaskPointMapper;
import com.example.xungeng2.pojo.TaskPoint;
import com.example.xungeng2.service.TaskPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TaskPointServiceImpl extends ServiceImpl<TaskPointMapper, TaskPoint> implements TaskPointService {

    @Autowired
    private TaskPointService taskPointService;

    @Autowired
    private TaskPointMapper taskPointMapper;
    @Override
    public List<TaskPoint> selectAllPoint() {

        List<TaskPoint> taskPoints = baseMapper.selectList(null);
        return taskPoints;
    }
    @Override
    public void deletePoint(Integer pointId) {
        taskPointService.removeById(pointId);
    }

    @Override
    public List<Integer> findPointIds(Integer userId) {
        List<TaskPoint> taskPoints = baseMapper.selectList(null);
        List<Integer> list22 = new ArrayList<>();
        for (TaskPoint tk: taskPoints) {
            list22.add(tk.getTaskId());
        }
        return list22;
    }

    @Override
    public TaskPoint selectPointById(Integer userId) {
        return taskPointMapper.findPointIds(userId);
    }

    @Override
    public int selectUserRWPointState(Integer userId, Integer taskId) {
        int i = taskPointMapper.selectRWPointState(userId, taskId);
        return i;
    }

    @Override
    public boolean clockPoint(int taskId, int userId) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        taskPointMapper.finishPoint(taskId, userId, df.format(new Date()));
        //修改打卡频次
        TaskPoint point = baseMapper.selectById(taskId);
        if (null == point){
            return false;
        }
        int frequency = point.getFrequency();
        point.setFrequency(++frequency);
        baseMapper.updateById(point);
        //巡更人员未完成的任务点数量减一
        int rwId = taskPointMapper.selectNowUserRWId(userId);
        taskPointMapper.updateNotNumByRWId(rwId);
        return true;
    }

    @Override
    public boolean selectUserPoint(String userId, String taskId) {
        Integer i = taskPointMapper.selectUserPoint(Integer.parseInt(userId), Integer.parseInt(taskId));
        if (i==0){
            return true;
        }
        return false;
    }

    @Override
    public void allotPoint(String user, String point) {
        int u = Integer.parseInt(user);
        int p = Integer.parseInt(point);
        taskPointMapper.allotPoint(u,p);
    }

    @Override
    public void changePointState(String taskId, String date) {
        taskPointMapper.changePointState(Integer.parseInt(taskId), date);
    }

    @Override
    public Page<TaskPoint> selectPageAll(Long i, Long i1) {
        Page<TaskPoint> pointPage = baseMapper.selectPage(new Page<>(i, i1), null);
        return pointPage;
    }

    @Override
    public void deleteThisPoint(TaskPoint taskPoint) {
        taskPointMapper.deleteById(taskPoint.getTaskId());
    }
}